package com.jyniubi.core.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyniubi.core.mapper.IntegralGradeMapper;
import com.jyniubi.core.pojo.entity.IntegralGrade;
import com.jyniubi.core.pojo.entity.UserInfo;
import com.jyniubi.core.service.IntegralGradeService;
import com.jyniubi.core.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author jyzhang
 * @since 2024-04-27
 */
@Service
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {

    @Resource
    private UserInfoService userInfoService;
    @Override
    public BigDecimal borrowAmount(Long userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        Integer integral = userInfo.getIntegral();
        IntegralGrade integralGrade = this.getOne(Wrappers.lambdaQuery(IntegralGrade.class)
                .le(IntegralGrade::getIntegralStart, integral)
                .ge(IntegralGrade::getIntegralEnd, integral));
        if(integralGrade==null){
            return BigDecimal.ZERO;
        }
        return integralGrade.getBorrowAmount();
    }
}
